package com.memory.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.memory.common.R;
import com.memory.daomain.Dish;
import com.memory.dto.DishDto;
import com.memory.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 邓哈哈
 * 2023/1/17 18:19
 * Function:
 * Version 1.0
 */
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 添加菜品
     * 添加口味
     *
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto) {
        Boolean saveDishWithFlavor = dishService.saveDishWithFlavor(dishDto);

        return saveDishWithFlavor ? R.success("添加成功") : R.error("添加失败");
    }

    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long ids) {
        Boolean delete = dishService.delete(ids);

        return delete ? R.success("删除成功") : R.error("删除失败");
    }

    /**
     * 更新菜品
     *
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> updateDishWithFlavor(@RequestBody DishDto dishDto) {
        Boolean updateDishWithFlavor = dishService.updateDishWithFlavor(dishDto);

        return updateDishWithFlavor ? R.success("更新成功") : R.error("更新失败");
    }

    /**
     * 按id查询(回显数据)
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getById(@PathVariable Long id) {
        DishDto dishDto = dishService.getById(id);

        return dishDto != null ? R.success(dishDto) : R.error("回显失败");
    }

    /**
     * 修改菜品状态(停售?启售)
     * 停售为 0,启售为 1
     *
     * @param status 判断菜品状态
     * @param ids    拿取菜品信息
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, Long ids) {
        Dish dish = dishService.getById(ids);

        if (status == 0)//对应菜品为启售状态
            dish.setStatus(0);//修改为停售状态
        else//对应菜品为停售状态
            dish.setStatus(1);//修改为启售状态
        //执行修改
        boolean updateById = dishService.updateById(dish);

        return updateById ? R.success("修改状态成功!") : R.error("修改状态失败");
    }

    /**
     * 分页查询
     * 条件查询
     *
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page<DishDto>> getPage(Integer page, Integer pageSize, Dish dish) {
        Page<DishDto> dishDtoPage = dishService.getPage(page, pageSize, dish);

        return (dishDtoPage != null) ? R.success(dishDtoPage) : R.error("页面显示失败");
    }

    /**
     * 根据菜品分类查询旗下所有菜品信息
     * 菜品展示
     *
     * @param dish
     * @return
     */
    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish) {
        List<DishDto> dishDtoList = dishService.list(dish);

        return dishDtoList != null ? R.success(dishDtoList) : R.error("查询出现错误");
    }
}
